﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESM.Objetos;

namespace ESMOffline
{
    public partial class SeleccionSE : Form
    {

        #region Propiedades Publicas y Privadas

        ESMOffline.ESMDataSet objESMDataSet = new ESMDataSet();
        int _seid = 0;
        int _medid = 0;
        Form _frmInicial = null;
        #endregion

        public SeleccionSE(Form frmInicial)
        {
            InitializeComponent();
            _frmInicial = frmInicial;

            this.FormClosed += new FormClosedEventHandler(SeleccionSE_FormClosed);
        }

        void SeleccionSE_FormClosed(object sender, FormClosedEventArgs e)
        {
            _frmInicial.Visible = true;
        }



        private void gvSeleccionEE_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.ColumnIndex == -1)
                    MessageBox.Show("Identificador no encontrado.");
                else if (e.ColumnIndex == 0)
                {
                    _seid = Convert.ToInt32(gvSeleccionSE.Rows[e.RowIndex].Cells[1].Value);

                    var ds_lecturacse = objESMDataSet.dbo_LecturaContextoSE;

                    ESMDataSetTableAdapters.dbo_LecturaContextoSETableAdapter objdbo_Mediciones_lc_TableAdapter = new ESMDataSetTableAdapters.dbo_LecturaContextoSETableAdapter();
                    objdbo_Mediciones_lc_TableAdapter.Fill(ds_lecturacse);

                    var med = (from m in ds_lecturacse
                               where m.IdSecretaria == _seid
                               select new { Medicion = m.IdMedicion }).Distinct();

                    //DataTable objTable = ds_evaluacion.Select("IdIE =" + _ieid).CopyToDataTable();
                    DataTable objTable = Application_ESM.ConvertToDataTable(med);

                    DataView objDataView = new DataView(objTable);
                    DataTable objDataTable = objDataView.ToTable(true, "Medicion");

                    gvMediciones.DataSource = objTable;
                    gvMediciones.Refresh();
                }
            }

            catch { MessageBox.Show("La secretaria de educación no tiene registro de mediciones.", "Información ESM", MessageBoxButtons.OK, MessageBoxIcon.Information); }
        }

        private void SeleccionSE_Load(object sender, EventArgs e)
        {
            var ds_se = objESMDataSet.dbo_Secretaria_Educacion;
            ESMDataSetTableAdapters.dbo_Secretaria_EducacionTableAdapter objdbo_dbo_Secretaria_EducacionTableAdapter = new ESMDataSetTableAdapters.dbo_Secretaria_EducacionTableAdapter();
            objdbo_dbo_Secretaria_EducacionTableAdapter.Fill(ds_se);

            gvSeleccionSE.AutoGenerateColumns = false;
            gvSeleccionSE.DataSource = ds_se.Select();
            gvSeleccionSE.Refresh();
        }

        private void gvMediciones_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.ColumnIndex == -1)
                    MessageBox.Show("Identificador no encontrado.");
                else if (e.ColumnIndex == 0)
                {
                    _medid = Convert.ToInt32(gvMediciones.Rows[e.RowIndex].Cells[1].Value);

                    new frmLecuraContextoSE(_seid, _medid).Show();
                }
            }
            catch (Exception)
            {

            }
        }


    }
}
